package com.gg.registration.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gg.common.result.CommonResult;
import com.gg.registration.hospital.feignClient.DictControllerClient;
import com.gg.registration.hospital.repository.HospitalRepository;
import com.gg.registration.hospital.service.HospitalService;
import com.gg.registration.model.hosp.Hospital;
import com.gg.registration.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjunjia
 * @date 2022/4/9 0:59
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    //这里是操作MongoDB的template，我们可以用mysql代替
    //但还是用MongoDB吧
    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictControllerClient dictControllerClient;

    /**
     * 上传医院接口
     */
    @Override
    public void save(Map<String, Object> paramMap) {

//        System.out.println(paramMap);
//        把参数map集合转换对象 Hospital
//        String mapString = JSONObject.toJSONString(paramMap);
//        System.out.println(mapString);
//        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        //使用ObjectMapper转对象吧
        ObjectMapper om = new ObjectMapper();
        Hospital hospital = om.convertValue(paramMap, Hospital.class);

        System.out.println(hospital);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在，进行修改
        if(hospitalExist != null) {
            hospitalExist.setStatus(hospitalExist.getStatus());
            hospitalExist.setUpdateTime(new Date());
            hospitalExist.setIsDeleted(0);
            hospitalRepository.save(hospitalExist);
        } else {//如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 展示医院信息接口
     * @return 医院实体类
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //添加的
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital.getHosname();
    }

    /**
     * 条件分页查询医院列表
     * @param currentPage 当前页
     * @return 统一返回结果集
     */
    @Override
    public Page<Hospital> getList(Integer currentPage,Integer limit,HospitalQueryVo hospitalQueryVo ) {
        Hospital hospital = new Hospital();
//        hospital.setHostype(hostype);
//        hospital.setDistrictCode(districtCode);
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建分页对象
        Pageable pageable = PageRequest.of(currentPage-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching().
                withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).
                withIgnoreCase(true);
        //创建条件对象
        Example<Hospital> example = Example.of(hospital,matcher);

        //调用方法执行分页，此时医院还没有等级信息，要在cmn微服务模块调用获得
        Page<Hospital> page = hospitalRepository.findAll(example, pageable);
        List<Hospital> hospitalList = page.getContent();

        for (Hospital hospital1 : hospitalList){
            getHospitalDetails(hospital1);
        }

        return page;
    }

    @Override
    public void updateStatus(String id,Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHospitalById(String id) {
        return this.getHospitalDetails(hospitalRepository.findById(id).get());
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 获取医院详情和预约详情
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.getHospitalDetails(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());

        return result;
    }

    private Hospital getHospitalDetails (Hospital hospital) {
        //远程调用CMN微服务 将数据字典的详情设置到医院中
        CommonResult hosType = dictControllerClient.getName("Hostype", hospital.getHostype());
        CommonResult province = dictControllerClient.getName(hospital.getProvinceCode());
        CommonResult city = dictControllerClient.getName( hospital.getCityCode());
        CommonResult district = dictControllerClient.getName(hospital.getDistrictCode());
        String addr = province.getData()+","+city.getData()+","+district.getData();

        hospital.getParam().put("hosType",hosType);
        hospital.getParam().put("addr",addr);
        return hospital;
    }
}












